<template>
	<view class="search-view">
		<u-search :showAction="true" actionText="搜索" :animation="true" v-model="title" @custom="searchcustom"></u-search>

		<template v-if="!isSearch">
			<view class="search-history">
				<h2>
					搜索历史
					<u-icon name="trash" size="17" color="#666" @click="deleteSearch"></u-icon>
				</h2>
				<view class="history-list">
					<span v-for="(item,index) in historyList" :key="index" @click="processingClicks(item)">{{item.search}}</span>
				</view>
			</view>
		</template>

		<template v-else>
			<view class="empty-box" v-if="searchList.length===0 && shopList.length == 0">
				<image :src="imgUrl+'empty-search.png'" mode=""></image>
				<p @tap="cancelSearch">返回</p>
			</view>
			<scroll-view v-else class="has-goods" scroll-y="true" lower-threshold="50" @scrolltolower="scrolltolower1">
				<view class="dianpuBox">
					<view class="dianpuItem flex_heng" v-for="(item,index) in shopList" :key="index" @tap="shopClick(item)">
						<image :src="item.icon" mode="" class="dianpuImg"></image>
						<view class="dianpuMid flex_shu">
							<view class="dianpuName u-line-1">
								{{item.shopName}}
							</view>
							<view class="dianpuSku u-line-2">
								索引：{{item.boothDadName}} {{item.boothName}}
							</view>
							<view class="dianpuSku u-line-2">
								营业时间:{{item.businessTime?`${item.businessTime.split(',')[0]}-${item.businessTime.split(',')[1]}`:''}}
							</view>
						</view>
					</view>
				</view>

				<u-line margin="20rpx 0rpx"></u-line>

				<view class="shopBox flex_heng">
					<view class="shopCard flex_shu" v-for="(item,index) in searchList" :key="index" @tap="goodsClick(item)">
						<image :src="item.pic" mode="" class="shopImg"></image>
						<view class="shopName u-line-2">
							{{item.title}}
						</view>
						<view class="shophengxian"></view>
						<view class="shopBtm flex_heng">
							<view class="shopPrice">
								￥{{item.price}}
							</view>
							<view class="shopStock">
								库存{{item.totalStock}}
							</view>
						</view>
					</view>
				</view>
				<u-loadmore loadmoreText="正在全力加载更多..." dashed line fontSize="21rpx" nomore-text="没有更多啦!"
					:status="moreStatus1" />
			</scroll-view>
		</template>
	</view>
</template>

<script>
	import {
		getProductList,
		getSearchList,
		deleteList,
		getShopList
	} from '@/api/api.js';
	export default {
		data() {
			return {
				imgUrl: this.$imgUrl,
				isSearch: false,
				searchKey: '',
				historyList: [],
				guessList: [],
				searchList: [],
				goodsList: [],
				shopList: [],
				moreStatus1: 'nomore',
				page: 1,
				total: 0,
				moreStatus2: 'nomore',
				page1: 1,
				total1: 0,
				title: ''
			}
		},
		onLoad() {
			this.getSearchHistory();
		},
		methods: {
			searchcustom() {
				this.getShopListAll();
				getProductList({
					params: {
						title: this.title
					}
				}).then(res => {
					console.log('res', res)
					this.isSearch = true;
					// this.searchList = [...this.searchList, ...res.rows];
					this.searchList = res.rows;
					this.total = res.total;
					console.log('searchList', this.searchList)
				})
			},

			//获取店铺列表
			getShopListAll() {
				getShopList({
					params: {
						shopName: this.title
					}
				}).then(res => {
					console.log('获取店铺列表', res)
					this.shopList = res.data;
				})
			},

			shopClick(item) {
				uni.navigateTo({
					url: `/package/pages/shopInfo/shopInfo?shopId=${item.id}`
				})
			},


			//商品详情
			goodsClick(item) {
				uni.navigateTo({
					url: `/package/pages/goodsDetail/goodsDetail?goodsId=${item.id}`
				})
			},

			// 删除历史搜索
			deleteSearch() {
				deleteList().then(res => {
					this.historyList = [];
					this.guessList = [];
					this.shopList = [];
				})
			},

			// 获取历史搜索
			getSearchHistory() {
				getSearchList({}).then(res => {
					console.log(res);
					this.historyList = res.data
					this.guessList = res.data.thinkSearch
				})
			},


			// 获取商品列表
			// getProductList(params) {
			// 	getProductList(params).then(res => {
			// 		console.log(res);
			// 		this.searchList = [...this.searchList, ...res.rows];
			// 		this.total = res.total;
			// 	}).finally(() => {
			// 		uni.hideLoading()
			// 	})
			// },

			// 滚动加载
			scrolltolower1() {
				if (this.total === this.searchList.length) {
					this.moreStatus1 = 'nomore'
					this.page = 1;
				} else {
					this.page += 1;
					this.moreStatus1 = 'loadmore'
					this.getList()
				}
			},

			cancelSearch() {
				this.isSearch = false
			},


			processingClicks(item) {
				this.title = item.search;
				this.searchcustom();
			},

			getList() {
				if (!this.searchKey) return;
				this.isSearch = true
				uni.showLoading({
					title: '加载中...',
					mask: true
				})
				this.searchcustom({
					pageNum: this.page,
					pageSize: 10,
					productName: this.searchKey,
				})
			}
		}
	}
</script>

<style lang="scss">
	.search-view {
		padding: 20rpx 28rpx;
		box-sizing: border-box;

		.u-search {
			// border: 1rpx solid #8551EB;

			.u-search__action {
				color: #000;
				line-height: 70rpx;
				width: 110rpx !important;
				margin-right: 4rpx;
				border-radius: 8rpx;
			}
		}

		.search-history {
			margin: 30rpx 0 42rpx;
			padding: 0 20rpx;
			box-sizing: border-box;

			h2 {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 30rpx;
				color: #2c2c2c;
				font-size: 32rpx;
				font-weight: 0;
				font-family: "PingFang-SC-Bold";
			}

			.history-list {
				display: flex;
				flex-wrap: wrap;

				span {
					padding: 4rpx 26rpx;
					box-sizing: border-box;
					margin-right: 20rpx;
					margin-bottom: 20rpx;
					background-color: #f2f2f2;
					font-size: 21rpx;
					color: #000000;
					font-family: 'hm-os-text';
					border-radius: 30rpx;
				}
			}
		}

		.guess-box {
			margin: 30rpx 0 42rpx;
			padding: 0 20rpx;
			box-sizing: border-box;

			h2 {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 30rpx;
				color: #2c2c2c;
				font-size: 32rpx;
				font-weight: 0;
				font-family: "PingFang-SC-Bold";
			}

			.guess-list {
				display: flex;
				flex-wrap: wrap;

				span {
					padding: 4rpx 26rpx;
					box-sizing: border-box;
					margin-right: 20rpx;
					margin-bottom: 20rpx;
					background-color: #f2f2f2;
					font-size: 21rpx;
					color: #000000;
					border-radius: 30rpx;
					font-family: 'hm-os-med';
				}
			}
		}

		.title {
			width: 356rpx;
			background-size: 100% 100% !important;
			font-size: 24rpx;
			color: #000000;
			font-family: 'hm-os-med';
			margin: 33rpx auto;
			text-align: center;
			position: relative;

			image {
				width: 100%;
				height: 4rpx;
				position: absolute;
				top: 50%;
				left: 0;
				transform: translateY(-50%);
			}
		}

		.no-goods {
			height: 55vh;
		}

		.has-goods {
			margin-top: 20rpx;
			height: 91vh;

			.dianpuBox {
				width: 100%;

				.dianpuItem {
					margin-bottom: 20rpx;
					justify-content: flex-start;

					.dianpuImg {
						width: 124rpx;
						height: 124rpx;
						border-radius: 16rpx;
					}

					.dianpuMid {
						margin-left: 16rpx;
						height: 124rpx;
						align-items: flex-start;
						justify-content: flex-start;
						font-family: "Alibaba PuHuiTi 2.0";

						.dianpuName {
							color: #000000;
							font-size: 28rpx;
							font-weight: 500;
						}

						.dianpuSku {
							color: #666666;
							font-size: 24rpx;
							font-weight: 400;
							margin-top: 8rpx;
						}
					}
				}
			}

			.shopBox {
				width: 100%;
				// padding: 28rpx 28rpx 0 28rpx;
				box-sizing: border-box;
				flex-wrap: wrap;


				.shopCard {
					width: 332rpx;
					height: 496rpx;
					margin-bottom: 20rpx;

					.shopImg {
						width: 322rpx;
						height: 334rpx;
						border-radius: 8rpx 8rpx 0 0;
					}

					.shopName {
						width: 100%;
						color: #000000;
						font-size: 28rpx;
						font-weight: 500;
						font-family: "Alibaba PuHuiTi 2.0";
						letter-spacing: 0.42rpx;
					}

					.shophengxian {
						width: 308rpx;
						height: 1rpx;
						opacity: 1;
						background: linear-gradient(90deg, #FFFFFF05 0%, #b3b3b34f 50%, #FFFFFF05 100%);
					}

					.shopBtm {
						width: 100%;
						height: 56rpx;

						.shopPrcie {
							color: #000000;
							font-size: 36rpx;
							font-weight: 700;
							font-family: "Alibaba PuHuiTi 2.0";
							letter-spacing: 0.54rpx;
						}

						.shopStock {
							color: #333333;
							font-size: 24rpx;
							font-weight: 400;
							font-family: "Alibaba PuHuiTi 2.0";
							letter-spacing: 0.36rpx;
						}
					}
				}
			}
		}

		.goods-item {
			display: flex;
			padding-bottom: 10rpx;
			border-bottom: 2rpx dashed #ddd;
			margin-bottom: 20rpx;

			.goods-img {
				width: 174rpx;
				height: 174rpx;
				margin-right: 20rpx;
			}

			.right {
				flex: 1;

				h2 {
					font-size: 28rpx;
					color: #000000;
					font-family: 'biaoti';
					display: flex;
					align-items: center;

					span {
						margin-left: 8rpx;
						padding: 4rpx 8rpx;
						box-sizing: border-box;
						color: #fff;
						font-size: 20rpx;
						font-family: 'hm-os-text';
						background-color: #8551EB;
						border-radius: 8rpx;
					}
				}

				p {
					font-size: 21rpx;
					color: #000000;
					font-family: 'hm-os-med';
					margin: 16rpx 0 51rpx;
				}

				.add-cart {
					display: flex;
					justify-content: space-between;
					align-items: center;

					.price {
						display: flex;
						align-items: center;

						b {
							font-size: 22rpx;
							color: #F21331;
							font-family: 'hm-os-label';
						}

						span {
							font-size: 40rpx;
							color: #F21331;
							font-family: 'biaoti';
							transform: translateY(-5rpx);
						}

						.ori-price {
							text-decoration: line-through;
							font-size: 24rpx;
							color: #666;
							font-family: 'hm-os-text';
							margin-left: 8rpx;
						}
					}

					image {
						width: 44rpx;
						height: 44rpx;
					}
				}
			}
		}
	}
</style>